﻿using CartoLab.GeometryLib.Geometries;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AEFunction.UI
{
    public partial class CreateFishnet : Form
    {
        private ILayer[] layers;
        public Coordinate startPt = new Coordinate();
        public ILayer selectLayer;
        public double width = 0;
        public double height = 0;
        public int rows = 0;
        public int cols = 0;
        public string FilePath = string.Empty;
        public CreateFishnet(ILayer[]l)
        {
            InitializeComponent();
            layers = l;
            foreach (ILayer item in layers)
            {
                NetExtentLayer.Items.Add(item.Name);
            }
            NetExtentLayer.SelectedIndex = 0;
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (WidthText.Text == "" || HeightText.Text == "")
            {
                MessageBox.Show("网格长、宽未设定。");
                return;}
            width = Convert.ToDouble(WidthText.Text);
            height = Convert.ToDouble(HeightText.Text);
            rows = Convert.ToInt32(RowText.Text);
            cols = Convert.ToInt32(ColText.Text);

            string strShapeFolder = System.IO.Path.GetDirectoryName(FilePath);
            string strShapeName = System.IO.Path.GetFileName(FilePath);
            esriGeometryType pShapeType = esriGeometryType.esriGeometryPolygon;
            ISpatialReference pSpatialReference = (selectLayer as IGeoDataset).SpatialReference;

           AEFunction.CreateShapefile(strShapeName, strShapeFolder, pShapeType, pSpatialReference, new List<IField>());


            this.DialogResult = DialogResult.OK;
        }

        private void NetExtentLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ILayer item in layers)
            {
                if(item.Name==NetExtentLayer.SelectedItem.ToString())
                {
                    selectLayer = item;
                    startPt.X= selectLayer.AreaOfInterest.UpperLeft.X;
                    startPt.Y = selectLayer.AreaOfInterest.UpperLeft.Y;
                    XCoorText.Text = startPt.X.ToString();
                    YCoorText.Text = startPt.Y.ToString();
                    break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "ShapeFiles文件(*.shp)|*.shp|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() != DialogResult.OK)
                return;
            FilePath = sfd.FileName;
            NetFileTextBox1.Text = FilePath;
        }
    }
}
